package order

import (
    "gorm.io/gorm"
    "ginweb/pkg/database"
)

type OrderRepository interface {
    CreateOrder(order *Order) error
    GetOrder(id string) (*Order, error)
    UpdateOrder(order *Order) error
    DeleteOrder(id string) error
}

type OrderRepositoryImpl struct {
    db *gorm.DB
}

func NewOrderRepository() *OrderRepositoryImpl {
    return &OrderRepositoryImpl{db: database.DB}
}

func (r *OrderRepositoryImpl) CreateOrder(order *Order) error {
    return r.db.Create(order).Error
}

func (r *OrderRepositoryImpl) GetOrder(id string) (*Order, error) {
    var order Order
    if err := r.db.First(&order, id).Error; err != nil {
        return nil, err
    }
    return &order, nil
}

func (r *OrderRepositoryImpl) UpdateOrder(order *Order) error {
    return r.db.Save(order).Error
}

func (r *OrderRepositoryImpl) DeleteOrder(id string) error {
    return r.db.Delete(&Order{}, id).Error
}